package yazan.com.pdacollector_android.presenter;

import com.blankj.utilcode.util.ToastUtils;

import java.util.HashMap;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import yazan.com.pdacollector_android.base.Presenter;
import yazan.com.pdacollector_android.network.Api;
import yazan.com.pdacollector_android.network.RetrofitManager;

public class AccountPresenter extends Presenter {

    public static void login(String baseUrl, String userName, String oldPassWord, String newPassWord, Integer type, final PresenterListener<HashMap>
            presenterListener) {
        Api api = null;
        try {
            api = RetrofitManager.getInstance()
                    .setBaseUrl(baseUrl)
                    .build(Api.class);
        } catch (Exception e) {
            ToastUtils.showShort(e.getMessage());
            return;
        }
        HashMap<String,Object> params = new HashMap<>();
        params.put("Fusr",userName);
        params.put("ForiPassword",oldPassWord);
        params.put("FnewPassword",newPassWord);
        params.put("Ftype",type);
        api.login(params).enqueue(new Callback<HashMap>() {
            @Override
            public void onResponse(Call<HashMap> call, Response<HashMap> response) {
                if (presenterListener == null) {
                    return;
                }
                if (response.isSuccessful()) {
                    presenterListener.onSuccess(response.body());
                } else {
                    presenterListener.onError(response.code() + "");
                }
            }

            @Override
            public void onFailure(Call<HashMap> call, Throwable t) {
                if (presenterListener == null) {
                    return;
                }
                presenterListener.onError(t.getMessage());
            }
        });
    }

    public static void changePassWord(String baseUrl, String userName, String oldPassWord, String newPassWord, Integer type, final
    PresenterListener<HashMap>
            presenterListener) {
        login(baseUrl, userName, oldPassWord, newPassWord, type, presenterListener);
    }
}
